package com.csw.mapreduce;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

//过滤
public class Demo06Sex {
    public static class Sex extends Mapper<LongWritable,Text,Text,NullWritable>{
        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            String line = value.toString();
            String sex = line.split(",")[3];
            if (sex.equals("男")){
                context.write(new Text(line),NullWritable.get());
            }
        }
    }
    public static void main(String[] args) throws Exception{
        //设置Job
        Job job = Job.getInstance();
        job.setJobName("sex");
        job.setJarByClass(Demo06Sex.class);
        //设置reduce task为0
        job.setNumReduceTasks(0);

        //设置输入和输出路径（输出路径存在则删除）
        FileInputFormat.addInputPath(job,new Path("/data/students.txt"));

        FileSystem fs = FileSystem.get(new Configuration());
        Path path = new Path("/data/sex");
        if (fs.exists(path)){
            fs.delete(path,true);
        }
        FileOutputFormat.setOutputPath(job,path);
        job.waitForCompletion(true);
    }
}
